﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using HK2009.Common;
using HK2009.BusinessFacade;
using HK2009.BusinessFacadeHandler;
using HK2009.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;

public partial class SystemManage_DestinationEdit : BasePage
{
    ISyDestinationHandler desManager = new SyDestinationHandler();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["UserID"] != null)
            {
                ISystemHandler sysManage = new SystemHandler();
                string strPermission = sysManage.GetLoginUserPermissions();
                if (!(strPermission.Contains("1024") || strPermission.Contains("1025")))
                {
                    Response.Redirect("~/NotPurview.aspx");
                    return;
                }
            }           

            dataBind();
        }
    }
    private void dataBind()
    {

        if (Request["PKID"] != null)
        {
            Sy_DestinationQuery query = desManager.GetDestinationByID(int.Parse(Request["PKID"]));
            if (query != null)
            {
                //txtCode.Text = query.Code;
                txtCountry.Text = query.Country;
                txtHKCountry.Text = query.HKCountry;
                txtENGCountry.Text = query.ENGCountry;
                txtCNName.Text = query.CNName;
                txtHKName.Text = query.HKName;
                txtENGName.Text = query.ENGName;
                if (query.Status ==0)
                {
                    radNo.Checked = true;
                    radYes.Checked = false;
                }
                else
                {
                    radNo.Checked = false;
                    radYes.Checked = true;
                }

            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (IsNameExists())
        {
            Jscript.Alert(Resources.Messages.NameExists, this.Page);
            return;
        }
        Sy_DestinationQuery desQuery = new Sy_DestinationQuery();

        desQuery.Country = txtCountry.Text;
        desQuery.HKCountry = txtHKCountry.Text;
        desQuery.ENGCountry = txtENGCountry.Text;

        desQuery.CNName = txtCNName.Text.Trim();
        desQuery.HKName = txtHKName.Text;
        desQuery.ENGName = txtENGName.Text;

        if (radYes.Checked)
        {
            desQuery.Status = 1;
        }
        else
        {
            desQuery.Status = 0;
        }
        desQuery.LastModUser = int.Parse(Session["UserID"].ToString());
        desQuery.LastModDate = DateTime.Now;

        try
        {
            if (Request["PKID"] != null)
            {
                Sy_DestinationQuery tmp = desManager.GetDestinationByID(int.Parse(Request["PKID"]));
                if (tmp == null)
                {
                    Jscript.Alert(Resources.Messages.RecordNotFound, this.Page);
                }
                else
                {
                    desQuery.PKID = tmp.PKID;
                    //desQuery.Code = tmp.Code;

                    if (desManager.UpdateDestination(desQuery))
                    {
                        Jscript.AlertAndRedirect(Resources.Messages.UpdateSuccess, "DestinationList.aspx", this.Page);
                    }
                    else
                    {
                        Jscript.Alert(Resources.Messages.UpdateFailed, this.Page);
                    }
                }
            }
            else
            {
                //desQuery.Code = txtCode.Text.Trim();

                if (desManager.AddDestination(desQuery))
                {
                    Jscript.AlertAndRedirect(Resources.Messages.AddSuccess, "DestinationList.aspx", this.Page);
                }
                else
                {
                    Jscript.Alert(Resources.Messages.AddFailed, this.Page);
                }
            }
        }
        catch
        {
            Jscript.Alert(Resources.Messages.SaveFailed, this.Page);
        }
        
    }

    private bool IsNameExists()
    {
        Sy_DestinationQuery query;
        string pkid = Request["PKID"];

        query = (pkid == null ? new Sy_DestinationQuery() : desManager.GetDestinationByID(int.Parse(pkid)));

        if (txtCNName.Text.Trim().Equals(query.CNName.Trim()) 
                && txtHKName.Text.Trim().Equals(query.HKName.Trim()) 
                && txtENGName.Text.Trim().Equals(query.ENGName.Trim()))
        {
            return false;
        }

        SqlParameter[] prams = new SqlParameter[4];
        prams[0] = new SqlParameter("@Country", txtCountry.Text.Trim());
        prams[1] = new SqlParameter("@CNName", txtCNName.Text.Trim());
        prams[2] = new SqlParameter("@HKName", txtHKName.Text.Trim());
        prams[3] = new SqlParameter("@ENGName", txtENGName.Text.Trim());
        string sql = "select * from Sy_Destination where Country = @Country and (CNName = @CNName or HKName = @HKName or ENGName = @ENGName)";
        DataTable dt = desManager.GetList(sql, prams);

        return dt != null && dt.Rows.Count > 0;
    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        Response.Redirect("DestinationList.aspx");
    }
}
